package com.lxmes.hr.service.impl;

import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcAbnormalAttend;
import com.lxmes.hr.mapper.NcAbnormalAttendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcAttendAbnormalMapper;
import com.lxmes.hr.domain.NcAttendAbnormal;
import com.lxmes.hr.service.INcAttendAbnormalService;
import javax.validation.Validator;

/**
 * 考勤异常Service业务层处理
 *
 * @author lx
 * @date 2023-04-06
 */
@Service
public class NcAttendAbnormalServiceImpl implements INcAttendAbnormalService
{
    @Autowired
    private NcAttendAbnormalMapper ncAttendAbnormalMapper;

    @Autowired
    private NcAbnormalAttendMapper ncAbnormalAttendMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询考勤异常
     *
     * @param id 考勤异常主键
     * @return 考勤异常
     */
    @Override
    public NcAttendAbnormal selectNcAttendAbnormalById(Long id)
    {
        return ncAttendAbnormalMapper.selectNcAttendAbnormalById(id);
    }

    /**
     * 查询考勤异常列表
     *
     * @param ncAttendAbnormal 考勤异常
     * @return 考勤异常
     */
    @Override
    public List<NcAttendAbnormal> selectNcAttendAbnormalList(NcAttendAbnormal ncAttendAbnormal)
    {
        return ncAttendAbnormalMapper.selectNcAttendAbnormalList(ncAttendAbnormal);
    }

    /**
     * 新增考勤异常
     *
     * @param ncAttendAbnormal 考勤异常
     * @return 结果
     */
    @Override
    public int insertNcAttendAbnormal(NcAttendAbnormal ncAttendAbnormal)
    {
        ncAttendAbnormal.setCreateTime(DateUtils.getNowDate());
        return ncAttendAbnormalMapper.insertNcAttendAbnormal(ncAttendAbnormal);
    }

    /**
     * 修改考勤异常
     *
     * @param ncAttendAbnormal 考勤异常
     * @return 结果
     */
    @Override
    public int updateNcAttendAbnormal(NcAttendAbnormal ncAttendAbnormal)
    {
        ncAttendAbnormal.setUpdateTime(DateUtils.getNowDate());
        return ncAttendAbnormalMapper.updateNcAttendAbnormal(ncAttendAbnormal);
    }

    /**
     * 批量删除考勤异常
     *
     * @param ids 需要删除的考勤异常主键
     * @return 结果
     */
    @Override
    public int deleteNcAttendAbnormalByIds(Long[] ids)
    {
        return ncAttendAbnormalMapper.deleteNcAttendAbnormalByIds(ids);
    }

    /**
     * 删除考勤异常信息
     *
     * @param id 考勤异常主键
     * @return 结果
     */
    @Override
    public int deleteNcAttendAbnormalById(Long id)
    {
        return ncAttendAbnormalMapper.deleteNcAttendAbnormalById(id);
    }
    @Override
    public String imporIteminfo(List<NcAttendAbnormal> ncAttendAbnormalList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncAttendAbnormalList) || ncAttendAbnormalList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcAttendAbnormal ncAttendAbnormal : ncAttendAbnormalList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncAttendAbnormal);
                ncAttendAbnormal.setCreateBy(operName);
                ncAttendAbnormal.setUpdateBy(operName);
                this.insertNcAttendAbnormal(ncAttendAbnormal);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int approvalAttAb(NcAttendAbnormal ncAttendAbnormal) {
        List<NcAttendAbnormal> ncAttendAbnormals = selectNcAttendAbnormalList(ncAttendAbnormal);
        NcAttendAbnormal ncAttendAbnormal1 = ncAttendAbnormals.get(0);
        updateNcAttendAbnormal(ncAttendAbnormal);
        NcAbnormalAttend ncAbnormalAttend = new NcAbnormalAttend();
        ncAbnormalAttend.setTenantId(ncAttendAbnormal1.getGcCode());
        ncAbnormalAttend.setGcName(ncAttendAbnormal1.getGcName());
        ncAbnormalAttend.setWorkNum(ncAttendAbnormal1.getJobNumber());
        ncAbnormalAttend.setKaoqinDate(ncAttendAbnormal1.getAbnormalDate());
        ncAbnormalAttend.setKaoqinType(ncAttendAbnormal.getKaoqinType());
        ncAbnormalAttend.setReason(ncAttendAbnormal.getReason());
        return ncAbnormalAttendMapper.insertNcAbnormalAttend(ncAbnormalAttend);
    }
}
